package com.wlstock.funi.utils;

import android.content.Context;

import com.wlstock.funi.dao.AIAccountDao;
import com.wlstock.funi.dao.MyAccountStockDao;
import com.wlstock.funi.dao.SelfFundSetDao;
import com.wlstock.funi.model.AIAccountsModel;
import com.wlstock.funi.model.MyAccountStocks;
import com.wlstock.funi.model.OperateModel;
import com.wlstock.funi.model.SelfFundModel;
import com.wlstock.funi.model.SelfFundSetModel;

import java.util.Date;

/**
 * Created by admin on 16/9/14.
 */

public class MyOwnStockManager {
    private Context mContext;
    private static MyOwnStockManager instance = null;

    private MyOwnStockManager(Context mContext) {
        this.mContext = mContext;
    }

    public static MyOwnStockManager getInstance(Context mContex) {
        if (instance == null) {
            synchronized (MyOwnStockManager.class) {
                if (instance == null) {
                    instance = new MyOwnStockManager(mContex);
                }
            }
        }
        return instance;
    }


    public void handleAIStock(SelfFundModel tmpSelfFundModel, AIAccountsModel aiStock){
        if (aiStock!=null) {
            tmpSelfFundModel.setMyStock(aiStock);
            aiStock.isRecord = "N";
            float ownFloatDis = CaculateUtils.getFloatDis(tmpSelfFundModel.getCurPrice(), Float.valueOf(aiStock.getCostPrice()));
            aiStock.setProfitRate(ownFloatDis);
            aiStock.setCurPrice(String.valueOf(tmpSelfFundModel.getCurPrice()));
            if (ownFloatDis < 0) {
                if (ownFloatDis < TradeSetUtils.getLossOffset(mContext, TradeSetUtils.LOSE_OFFSET)) {
                    // 达到止损条件(立即止损) 或 延时止损,大盘小于2695的而且在监测时间内则执行卖出操作
                    SellHelper.getInstance(mContext).orderAISaile(tmpSelfFundModel, aiStock, TradeSetUtils.LOSE_OFFSET);
                }
            }else {
                if (ownFloatDis > aiStock.gethProfiteRate()) {
                    aiStock.sethProfiteRate(ownFloatDis);
                }
                if (ownFloatDis > TradeSetUtils.getLossOffset(mContext, TradeSetUtils.WIN_OFFSET)) {
                    SellHelper.getInstance(mContext).orderAISaile(tmpSelfFundModel, aiStock, TradeSetUtils.WIN_OFFSET);
                }
            }

            if (SelfFundSetDao.getInstance(mContext).isSaile(tmpSelfFundModel, new Date())) {
                //在制定的时间去到指定的价格执行卖出操作
                SellHelper.getInstance(mContext).orderAISaile(tmpSelfFundModel, aiStock, "INTIME");
            }
            AIAccountDao.getInstance(mContext).update(aiStock);
//						NotiUtils.showMyAccontMode(mContext, aiStock, "NORMAL");
            NotiUtils.showMyAccountMode(mContext, aiStock, tmpSelfFundModel);
        }

    }



    public void handlRealStock(SelfFundModel tmpSelfFundModel, MyAccountStocks ownStock, String isRecord) {
        if (ownStock!=null) {
            tmpSelfFundModel.setMyStock(ownStock);
            ownStock.isRecord = isRecord;
            float ownFloatDis = CaculateUtils.getFloatDis(tmpSelfFundModel.getCurPrice(), Float.valueOf(ownStock.getCostPrice()));
            ownStock.setProfitRate(ownFloatDis);
            ownStock.setCurPrice(String.valueOf(tmpSelfFundModel.getCurPrice()));
            if (ownFloatDis < 0) {
                if (ownFloatDis < TradeSetUtils.getLossOffset(mContext, TradeSetUtils.LOSE_OFFSET)) {
                    SellHelper.getInstance(mContext).orderSaile(tmpSelfFundModel,ownStock,"执行情绪止损操作");
                    NotiUtils.showMyAccontMode(mContext,ownStock,TradeSetUtils.LOSE_OFFSET);
                }
            }else {
                if (ownFloatDis > ownStock.gethProfiteRate()) {
                    ownStock.sethProfiteRate(ownFloatDis);
                    MyAccountStockDao.getInstance(mContext).updateHighestProRate(ownStock.getStkCode(), ownFloatDis);
                }
                if (ownFloatDis > TradeSetUtils.getLossOffset(mContext, TradeSetUtils.WIN_OFFSET)) {
                    //达到止盈条件
                    SellHelper.getInstance(mContext).orderSaile(tmpSelfFundModel,ownStock,"执行止盈操作");
                    NotiUtils.showMyAccontMode(mContext,ownStock,TradeSetUtils.WIN_OFFSET);
                }
            }

            if (TimeUtils.isInNeedBackTime() && SellHelper.getInstance(mContext).isNeedRebackSaile(ownFloatDis, ownStock.gethProfiteRate())){
                //是否移动止盈止损
                SellHelper.getInstance(mContext).orderSaile(tmpSelfFundModel,ownStock,"执行移动止盈操作");
                NotiUtils.showMyAccontMode(mContext,ownStock,TradeSetUtils.WIN_OFFSET);
            }

            SelfFundSetModel sfSetModel = SelfFundSetDao.getInstance(mContext).needOpSetModel(tmpSelfFundModel, new Date(), OperateModel.TradeType_SELL);
            if (sfSetModel!=null) {
                //在制定的时间去到指定的价格执行卖出操作
                float tradePrice = getSelfSetModelPrice(sfSetModel,tmpSelfFundModel);
                tmpSelfFundModel.setCurPrice(tradePrice);
                SellHelper.getInstance(mContext).orderSaile(tmpSelfFundModel, ownStock, "在指定时间内到达指定价格，执行卖出操作");
                NotiUtils.showMyAccontMode(mContext, ownStock, "INTIME");
            }
//						NotiUtils.showMyAccontMode(mContext, ownStock, "NORMAL");
            NotiUtils.showMyAccountMode(mContext, ownStock, tmpSelfFundModel);
        }
    }


    //获取设置中的价格
    public float getSelfSetModelPrice(SelfFundSetModel sfSetModel,SelfFundModel tmpSelfFundModel) {
        if (sfSetModel.getPrice() == SelfFundSetModel.OPEN_PRICE) {
            return tmpSelfFundModel.getCurOpen();
        }else {
            return tmpSelfFundModel.getCurPrice();
        }
    }
}
